package test.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class ItemDao {

    public static ResultSet selectItem(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
        System.out.println("请输入你所要查的试题科目：");
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next();
        String[] strings = {next};
        String sql = "select * from item_bank where subject = ?";
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < strings.length; i++) {
            preparedStatement.setObject(i + 1, strings[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    public static int updateItem(Connection connection, PreparedStatement preparedStatement) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        System.out.println("1.增2.删3.改");
        int i = scanner.nextInt();
        int j = 0;
        if (i == 1) {
            System.out.println("请输入试题内容：");
            String s1 = scanner.next();
            System.out.println("请输入试题类型：");
            String s2 = scanner.next();
            System.out.println("请输入试题科目");
            String s3 = scanner.next();
            String[] strings = {s1, s2, s3};
            String sql = "insert into item_bank (content,type,subject) values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            for (int k = 0; k < strings.length; k++) {
                preparedStatement.setObject(k + 1, strings[k]);
            }
            j = preparedStatement.executeUpdate();
            if (j == 0) {
                System.out.println("增加失败");
            }
            return j;
        }
        if (i == 2) {
            System.out.println("请输入你要删除的试题id");
            int nextInt = scanner.nextInt();
            String sql = "delete from item_bank where id = ? ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setObject(1, nextInt);
            j = preparedStatement.executeUpdate();
            if (j == 0) {
                System.out.println("删除失败");
            }
            return j;
        }
        if (i == 3) {
            System.out.println("请输入你要修改的试题id");
            int nextInt = scanner.nextInt();
            String sql1 = "";
            String sql2 = "select * from item_bank where id =?";
            preparedStatement = connection.prepareStatement(sql2);
            preparedStatement.setObject(1, nextInt);
            ResultSet rs = preparedStatement.executeQuery();
            if (rs.next()) {
                System.out.println("该试题全部信息如下：");
                System.out.println("id:" + rs.getInt("id") + " 内容:" + rs.getString("content") + " 类型:" + rs.getString("type"));
            }
            System.out.println("请输入你要修改的信息：1.内容2.类型");
            int next = scanner.nextInt();
            if (next == 1) {
                sql1 = "update item_bank set content = ? where id = ?";
                preparedStatement = connection.prepareStatement(sql1);
                System.out.println("请输入新的内容：");
                String next1 = scanner.next();
                preparedStatement.setObject(1, next1);
                preparedStatement.setObject(2, nextInt);
            } else {
                sql1 = "update item_bank set type = ? where id = ?";
                preparedStatement = connection.prepareStatement(sql1);
                System.out.println("请输入新的类型");
                String next2 = scanner.next();
                preparedStatement.setObject(1, next2);
                preparedStatement.setObject(2, nextInt);
            }
            j = preparedStatement.executeUpdate();
            if(j==0){
                System.out.println("修改失败");
            }
            return j;
        }
        return j;
    }

}
